Page({
  data: {
    currentTab: 0, // 0处理中 1历史
    orderList: [],
    statusMap: {
      pending: '待支付',
      processing: '进行中',
      completed: '已完成',
      cancelled: '已取消'
    }
  },
  onShow() {
    this.fetchOrders();
  },
  switchTab(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({ currentTab: index });
    this.fetchOrders();
  },
  fetchOrders() {
    const userId = wx.getStorageSync('userId');
    if (!userId) {
      wx.showToast({ title: '请先登录', icon: 'none' });
      return;
    }
    const app = getApp();
    if (this.data.currentTab == 0) {
      // 处理中：查pending和processing
      const pendingReq = app.request({
        url: `/api/orders/user/${userId}/status/pending`,
        method: 'GET'
      });
      const processingReq = app.request({
        url: `/api/orders/user/${userId}/status/processing`,
        method: 'GET'
      });
      Promise.all([pendingReq, processingReq])
        .then(([pending, processing]) => {
          this.setData({ 
            orderList: [...(pending.data || []), ...(processing.data || [])] 
          });
        })
        .catch(err => {
          console.error('获取订单列表失败:', err);
          wx.showToast({
            title: '获取订单列表失败',
            icon: 'none'
          });
        });
    } else {
      // 历史订单：查completed
      app.request({
        url: `/api/orders/user/${userId}/status/completed`,
        method: 'GET'
      })
        .then(res => {
          this.setData({ orderList: res.data || [] });
        })
        .catch(err => {
          console.error('获取历史订单失败:', err);
          wx.showToast({
            title: '获取历史订单失败',
            icon: 'none'
          });
          this.setData({ orderList: [] });
        });
    }
  },
  viewDetail(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?orderId=${orderId}`
    });
  },
  payOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/payment/payment?orderId=${orderId}`
    });
  },
  cancelOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认取消',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp();
          app.request({
            url: `/api/orders/${orderId}/cancel`,
            method: 'PUT'
          })
            .then(() => {
              wx.showToast({
                title: '订单已取消',
                icon: 'success'
              });
              this.fetchOrders();
            })
            .catch(err => {
              console.error('取消订单失败:', err);
              wx.showToast({
                title: '取消订单失败',
                icon: 'none'
              });
            });
        }
      }
    });
  },
  confirmOrder(e) {
    wx.showToast({ title: '确认收货功能待实现', icon: 'none' });
  },
  reviewOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/review/review?orderId=${orderId}`
    });
  }
}); 